/*
 *
 * This is a VoIP Application designed for local-network usage for voice and
 * messaging
 * Copyright (C) 2012  Imran M Yousuf (imyousuf@smitsol.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.smitsol.voip.api.impl;

import com.google.inject.Inject;
import com.smartitengineering.dao.common.CommonReadDao;
import com.smartitengineering.dao.common.CommonWriteDao;
import com.smartitengineering.dao.common.queryparam.QueryParameter;
import com.smartitengineering.dao.common.queryparam.QueryParameterFactory;
import com.smitsol.voip.api.BindingRegistryService;
import com.smitsol.voip.api.domains.BindingRegistry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author russel
 */
public class BindingRegistryServiceImpl implements BindingRegistryService {

  @Inject
  private CommonReadDao<BindingRegistry, String> commonReadDao;
  @Inject
  private CommonWriteDao<BindingRegistry> commonWriteDao;

  public void registerBinding(BindingRegistry persistentBinding) {
    commonWriteDao.save(persistentBinding);
  }

  public void update(BindingRegistry persistentBinding) {
    commonWriteDao.update(persistentBinding);
  }

  public void delete(BindingRegistry persistentBinding) {
    commonWriteDao.delete(persistentBinding);
  }

  public Collection<String> getAorsByUsername(String username) {
    List<QueryParameter> qps = new ArrayList<QueryParameter>();
    qps.add(QueryParameterFactory.getEqualPropertyParam("username", username));
    List<BindingRegistry> bindings = commonReadDao.getList(qps);
    List<String> aors = new ArrayList<String>();
    for (BindingRegistry persistentBinding : bindings) {
      aors.add(persistentBinding.getAddressOfRecord());
    }
    return aors;
  }

  public Collection<BindingRegistry> getByUsername(String username) {
    List<QueryParameter> qps = new ArrayList<QueryParameter>();
    qps.add(QueryParameterFactory.getEqualPropertyParam("username", username));
    return commonReadDao.getOtherList(qps);
  }
}
